package com.gs.dao.jdbc.impl;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Controller;

import com.gs.action.datamodel.UserDataModel;
import com.gs.bean.User;
import com.gs.dao.UserDAO;
import com.gs.dao.jdbc.impl.rowmapper.UserRowMapper;
import com.liang.log.Log;
import com.liang.util.Page;

@Controller(value = "userDAOImpl")
public class UserDAOImpl extends BaseDAOImpl implements UserDAO {

	public List<User> queryAll() {
		List<User> users = null;
		String sql = "select * from user_info";
		users = jdbcTemplate.query(sql, new UserRowMapper());

		if (users == null) {
			users = new ArrayList<User>(0);
		}
		return users;
	}

	private int[] getArgTypes(List<Integer> argTypeObjs) {
		int[] _argTypes = new int[argTypeObjs.size()];
		for (int i = 0; i < argTypeObjs.size(); i++) {
			_argTypes[i] = argTypeObjs.get(i);
		}
		return _argTypes;
	}


	@Override
	public void deleteModel(Integer id) throws Exception {
		String sql = "delete from user_info where id = ?";
		jdbcTemplate.update(sql, new Object[] { id },
				new int[] { Types.INTEGER });
	}

	@Override
	public void deleteModel(User model) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public User findModel(Integer id) throws Exception {
		String sql = "select * from user_info where id = ?";
		return (User) jdbcTemplate.queryForObject(sql, new Object[] { id },
				new UserRowMapper());
	}

	@Override
	public void saveModel(User newUser) throws Exception {
		String sql = "insert into user_info(name,password) values(?,?)";
		jdbcTemplate.update(sql, new Object[] { newUser.getName(),
				newUser.getPassword() }, new int[] { Types.VARCHAR,
				Types.VARCHAR });
	}

	@Override
	public void updataModel(User model) throws Exception {
		String sql = "update user_info where";
		if (model.getName() != null) {
			sql += "name = " + model.getName();
		}
		if (model.getPassword() != null) {
			sql += "passwrord = " + model.getPassword();
		}
		jdbcTemplate.update(sql);
	}

	@Override
	public Page<User> queryPage(UserDataModel searchModel, int currentPage,
			int pageSize) throws Exception {
		String _queryQuery = "select t.* from (select top "
				+ (currentPage * pageSize) + " * from user_info";
		String _queryCount = "select count(id) from user_info";
		StringBuffer _queryWhere = new StringBuffer();

		List<User> users = null;
		int total = 0;
		if (null == searchModel || searchModel.getUser() == null) {
			_queryQuery += " ) t";
			if (currentPage > 1) {
				_queryQuery += " left join (select top "
						+ ((currentPage - 1) * pageSize)
						+ " * from user_info) tt on t.id = tt.id where iif(tt.id , '0', '1') = '1'";
			}
			users = jdbcTemplate.query(_queryQuery, new UserRowMapper());
			total = jdbcTemplate.queryForInt(_queryCount);
		} else {
			List<Object> _args = new ArrayList<Object>(0);
			List<Integer> _argTypeObjs = new ArrayList<Integer>(0);
			
			User user= searchModel.getUser();
			
			if (user.getId() != null) {
				_queryWhere.append(" and id= ?");
				_args.add(user.getId());
				_argTypeObjs.add(Types.INTEGER);
			}
			if (user.getName() != null) {
				_queryWhere.append(" and name like ?");
				_args.add('%' + user.getName() + '%');
				_argTypeObjs.add(Types.VARCHAR);
			}
			if (user.getPassword() != null) {
				_queryWhere.append(" and password like ?");
				_args.add('%' + user.getPassword() + '%');
				_argTypeObjs.add(Types.VARCHAR);
			}

			if (!_queryWhere.toString().equals("")) {
				_queryWhere.delete(0, 4);
				_queryWhere.insert(0, " where");
			}
			_queryCount += _queryWhere.toString();
			Log.info("sql count:" + _queryCount);
			total = jdbcTemplate.queryForInt(_queryCount, _args.toArray(),
					getArgTypes(_argTypeObjs));

			_queryQuery += _queryWhere.toString() + " ) t";
			if (currentPage > 1) {
				_queryQuery += " left join (select top "
						+ ((currentPage - 1) * pageSize)
						+ " * from user_info"
						+ _queryWhere.toString()
						+ ") tt on t.id = tt.id where iif(tt.id , '0', '1') = '1'";

				List<Object> _copyArgs = new ArrayList<Object>(_args);
				_args.addAll(_copyArgs);
				List<Integer> _copyArgTypeObjs = new ArrayList<Integer>(
						_argTypeObjs);
				_argTypeObjs.addAll(_copyArgTypeObjs);
			}
			int[] _argTypes = new int[_argTypeObjs.size()];
			for (int i = 0; i < _argTypeObjs.size(); i++) {
				_argTypes[i] = _argTypeObjs.get(i);
			}
			users = jdbcTemplate.query(_queryQuery, _args.toArray(), _argTypes,
					new UserRowMapper());

			_queryWhere = null;
			_args = null;
			_argTypeObjs = null;
		}

		if (null == users) {
			users = new ArrayList<User>(0);
		}

		Page<User> pageModel = new Page<User>(users, currentPage, pageSize,
				total);
		return pageModel;
	}

}
